// pages/shoppingCar/shoppingCar.js
import checkAuth from '../../utils/checkAuth'
import { getCartAndGoodData, modifyCartsData, getCartData, deleteCartData } from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    slideButtons: [
      {
        type: 'warn',
        text: '删除',
      }
    ],
    cartList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    checkAuth(() => {
      const username = wx.getStorageSync('token').nickName;
      const tel = wx.getStorageSync('tel');
      getCartAndGoodData({
        username,
        tel
      }).then(res => {
        console.log(res.data);
        this.setData({
          cartList: res.data
        })
      })
      console.log('显示购物车')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 左滑按钮点击事件
  slideButtonTap(e) {
    console.log('slide button tap', e.detail)
  },
  // 加按钮
  increment(e) {
    const { currentTarget: { dataset: { goodnum, goodid } } } = e;
    console.log(goodid, goodnum);
    const username = wx.getStorageSync('token').nickName;
    const tel = wx.getStorageSync('tel');
    getCartData({
      goodId: goodid,
      username,
      tel
    }).then(res => {
      let goodData = res.data;
      console.log(goodData, res);
      return modifyCartsData({
        ...goodData[0],
        number: goodnum + 1
      })
    }).then(res => {
      return getCartAndGoodData({
        username,
        tel
      })
    }).then(res => {
      this.setData({
        cartList: res.data
      })
    }).catch(err => {
      console.log(err);
    })
  },
  // 减按钮
  decrement(e) {
    const { currentTarget: { dataset: { goodnum, goodid } } } = e;
    console.log(goodid, goodnum);
    const username = wx.getStorageSync('token').nickName;
    const tel = wx.getStorageSync('tel');
    getCartData({
      goodId: goodid,
      username,
      tel
    }).then(res => {
      let goodData = res.data;
      console.log(goodData, res);
      return modifyCartsData({
        ...goodData[0],
        number: goodnum - 1
      })
    }).then(res => {
      return getCartAndGoodData({
        username,
        tel
      })
    }).then(res => {
      this.setData({
        cartList: res.data
      })
    }).catch(err => {
      console.log(err);
    })
  },
  // 复选框选中
  checkboxChange(e) {
    const username = wx.getStorageSync('token').nickName;
    const tel = wx.getStorageSync('tel');
    const { currentTarget: { dataset: { gooditem } } } = e;
    console.log(gooditem);
    modifyCartsData({
      ...gooditem,
      checked: !gooditem.checked
    }).then(res => {
      return getCartAndGoodData({
        username,
        tel
      })
    }).then(res => {
      this.setData({
        cartList: res.data
      })
    }).catch(err => {
      console.log(err);
    })
  },
  // 删除商品
  slideButtonTap(e) {
    const { currentTarget: { dataset: { gooditem } } } = e;
    const username = wx.getStorageSync('token').nickName;
    const tel = wx.getStorageSync('tel');
    deleteCartData(gooditem).then(res => {
      return getCartAndGoodData({
        username,
        tel
      })
    }).then(res => {
      this.setData({
        cartList: res.data
      })
    }).catch(err => {
      console.log(err);
    })
  },
  // 判断全选/全不选
  estimateCheckd(arr) {
    for (var i = 0; i < arr.length; i++) {
      if (!arr[i].checked) {
        return false
      }
    }
    return true
  },
  // 全选/全不选
  selectAll(e) {
    const { currentTarget: { dataset: { cartlist } } } = e;
    const isChecked = this.estimateCheckd(cartlist);
    const username = wx.getStorageSync('token').nickName;
    const tel = wx.getStorageSync('tel');
    let tempArr=[];
    cartlist.forEach(item => {
      tempArr.push(modifyCartsData({...item,
        checked:!isChecked}))
    });
    Promise.all(tempArr).then(res=>{
      console.log(res);
      return getCartAndGoodData({
        username,
        tel
      })
    }).then(res=>{
      this.setData({
        cartList: res.data
      })
    }).catch(err=>{
      console.log(err);
    });
  }
})